package com.kang.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kang.vo.FriendGroupVo;
import com.kang.vo.FriendVo;
import com.kang.bean.Friend;
import com.kang.bean.FriendGroup;
import com.kang.mapper.FriendGroupMapper;
import com.kang.mapper.FriendMapper;
import com.kang.service.FriendService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @program: photo_fawu
 * @description:
 * @author: K.faWu
 * @create: 2021-11-09 10:50
 **/
@Service
public class FriendServiceImpl extends ServiceImpl<FriendMapper, Friend> implements FriendService {

    @Autowired
    private FriendMapper friendMapper;
    @Autowired
    private FriendGroupMapper friendGroupMapper;

    @Override
    public List<FriendGroupVo> getByUserId(Long userId) {
        ArrayList<FriendGroupVo> friendGroupVos = new ArrayList<>();
        // 获取好友分组
        QueryWrapper<FriendGroup> friendGroupQueryWrapper = new QueryWrapper<>();
        friendGroupQueryWrapper.eq("user_id", userId);
        List<FriendGroup> friendGroups = friendGroupMapper.selectList(friendGroupQueryWrapper);
        if (friendGroups == null || friendGroups.size()==0){
            return null;
        }
        // 获取每个分组的用户
        friendGroups.forEach(friendGroup -> {
            FriendGroupVo friendGroupVo = new FriendGroupVo();
            BeanUtils.copyProperties(friendGroup,friendGroupVo);
            List<FriendVo> list = friendMapper.selectAndHeadPortrait(friendGroupVo.getId(), userId);
            friendGroupVo.setFriends(list);
            friendGroupVos.add(friendGroupVo);
        });

        return friendGroupVos;
    }

    @Override
    public void addFriend(Friend friend) {
        friendMapper.insert(friend);
    }
}
